package com.wlz.util;

import org.apache.commons.codec.digest.DigestUtils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 *  md5 算法
 * @author wlz
 * @date 2022-11-28  1:45 下午
 */
public class SHA256Utils {

    public static void main(String[] args) throws Exception {
        // 对文件 sha256
       sha256File();

        // 对数据sha256
//        sha256Data();
    }

    private static void sha256Data() {
        long l = System.currentTimeMillis();
        String s = "{\n" +
                "    \"projectCode\": \"C46AB-SWM-TEST\",\n" +
                "    \"copyrightNum\": \"2023020917581222\",\n" +
                "    \"machineVersion\": \"Arcfox-V3.0.0\",\n" +
                "    \"partCollection\": [\n" +
                "        {\n" +
                "            \"partCode\": \"E00126678\",\n" +
                "            \"partName\": \"TBX\",\n" +
                "            \"controllerCode\": \"TBX\",\n" +
                "            \"softVersion\": \"S000007\",\n" +
                "            \"calibratedVersion\": \"\",\n" +
                "            \"systemVersion\": \"001\",\n" +
                "            \"softName\": \"TBXE00126678S000007N60ABS000006.zip\",\n" +
                "            \"softUrl\": null,\n" +
                "            \"md5\": null\n" +
                "        },\n" +
                "        {\n" +
                "            \"partCode\": \"A00118616\",\n" +
                "            \"partName\": \"DSM\",\n" +
                "            \"controllerCode\": \"DSM\",\n" +
                "            \"softVersion\": \"S002000\",\n" +
                "            \"calibratedVersion\": \"\",\n" +
                "            \"systemVersion\": \"001\",\n" +
                "            \"softName\": \"DSMA00118616S002000C62X.zip\",\n" +
                "            \"softUrl\": null,\n" +
                "            \"md5\": null\n" +
                "        },\n" +
                "        {\n" +
                "            \"partCode\": \"A00119270\",\n" +
                "            \"partName\": \"HUM\",\n" +
                "            \"controllerCode\": \"HUM\",\n" +
                "            \"softVersion\": \"S000040\",\n" +
                "            \"calibratedVersion\": \"\",\n" +
                "            \"systemVersion\": \"001\",\n" +
                "            \"softName\": \"HUMA00119270S000040C62XS000030.zip\",\n" +
                "            \"softUrl\": null,\n" +
                "            \"md5\": null\n" +
                "        }\n" +
                "    ]\n" +
                "}";
        String sha256 = DigestUtils.sha256Hex(s);
        System.out.println("sha256_1 = " + sha256);
        System.out.println("sha256_1耗时: " + (System.currentTimeMillis() - l));


        long l_2 = System.currentTimeMillis();
        String s2 = "{\n" +
                "    \"projectCode\": \"C46AB-SWM-TEST\",\n" +
                "    \"copyrightNum\": \"2023020917581222\",\n" +
                "    \"machineVersion\": \"Arcfox-V3.0.0\",\n" +
                "    \"partCollection\": [\n" +
                "        {\n" +
                "            \"partCode\": \"E00126678\",\n" +
                "            \"partName\": \"TBX\",\n" +
                "            \"controllerCode\": \"TBX\",\n" +
                "            \"softVersion\": \"S000007\",\n" +
                "            \"calibratedVersion\": \"\",\n" +
                "            \"systemVersion\": \"001\",\n" +
                "            \"softName\": \"TBXE00126678S000007N60ABS000006.zip\",\n" +
                "            \"softUrl\": null,\n" +
                "            \"md5\": null\n" +
                "        },\n" +
                "        {\n" +
                "            \"partCode\": \"A00118616\",\n" +
                "            \"partName\": \"DSM\",\n" +
                "            \"controllerCode\": \"DSM\",\n" +
                "            \"softVersion\": \"S002000\",\n" +
                "            \"calibratedVersion\": \"\",\n" +
                "            \"systemVersion\": \"001\",\n" +
                "            \"softName\": \"DSMA00118616S002000C62X.zip\",\n" +
                "            \"softUrl\": null,\n" +
                "            \"md5\": null\n" +
                "        },\n" +
                "        {\n" +
                "            \"partCode\": \"A00119270\",\n" +
                "            \"partName\": \"HUM\",\n" +
                "            \"controllerCode\": \"HUM\",\n" +
                "            \"softVersion\": \"S000040\",\n" +
                "            \"calibratedVersion\": \"\",\n" +
                "            \"systemVersion\": \"001\",\n" +
                "            \"softName\": \"HUMA00119270S000040C62XS000030.zip\",\n" +
                "            \"softUrl\": null,\n" +
                "            \"md5\": null\n" +
                "        }\n" +
                "    ]\n" +
                "}";
        String sha256_2 = DigestUtils.sha256Hex(s2);
        System.out.println("sha256_2 = " + sha256_2);
        System.out.println("sha256_2耗时: " + (System.currentTimeMillis() - l_2));

        long l_3 = System.currentTimeMillis();
        String s3 = "{\n" +
                "    \"projectCode\": \"C46AB-SWM-TEST\",\n" +
                "    \"copyrightNum\": \"2023020917581222\",\n" +
                "    \"machineVersion\": \"Arcfox-V3.0.0\",\n" +
                "    \"partCollection\": [\n" +
                "        {\n" +
                "            \"partCode\": \"E00126678\",\n" +
                "            \"partName\": \"TBX\",\n" +
                "            \"controllerCode\": \"TBX\",\n" +
                "            \"softVersion\": \"S000007\",\n" +
                "            \"calibratedVersion\": \"\",\n" +
                "            \"systemVersion\": \"001\",\n" +
                "            \"softName\": \"TBXE00126678S000007N60ABS000006.zip\",\n" +
                "            \"softUrl\": null,\n" +
                "            \"md5\": null\n" +
                "        },\n" +
                "        {\n" +
                "            \"partCode\": \"A00118616\",\n" +
                "            \"partName\": \"DSM\",\n" +
                "            \"controllerCode\": \"DSM\",\n" +
                "            \"softVersion\": \"S002000\",\n" +
                "            \"calibratedVersion\": \"\",\n" +
                "            \"systemVersion\": \"001\",\n" +
                "            \"softName\": \"DSMA00118616S002000C62X.zip\",\n" +
                "            \"softUrl\": null,\n" +
                "            \"md5\": null\n" +
                "        },\n" +
                "        {\n" +
                "            \"partCode\":          \"A00119270\",\n" +
                "            \"partName\": \"HUM\",\n" +
                "            \"controllerCode\": \"HUM\",\n" +
                "            \"softVersion\":     \"S000040\",\n" +
                "            \"calibratedVersion\": \"\",\n" +
                "            \"systemVersion\": \"001\",\n" +
                "            \"softName\": \"HUMA00119270S000040C62XS000030.zip\",\n" +
                "            \"softUrl\": null,\n" +
                "            \"md5\": null\n" +
                "        }\n" +
                "    ]\n" +
                "}";
        String sha256_3 = DigestUtils.sha256Hex(s3);
        System.out.println("sha256_3 = " + sha256_3);
        System.out.println("sha256_3耗时: " + (System.currentTimeMillis() - l_3));
    }

    private static void sha256File() throws Exception {
        long l = System.currentTimeMillis();
        String path = "/Users/wlz/Desktop/987abf77-c02c-4612-a53e-6e62db3ff074.zip";
        String sha256 = DigestUtils.sha256Hex(new FileInputStream(path));
        System.out.println("sha256_1 = " + sha256);
        System.out.println("sha256_1耗时: " + (System.currentTimeMillis() - l));

//        long l_2 = System.currentTimeMillis();
//        String path2 = "/Users/wlz/Desktop/6be842c8ae014d32a3d327b367b8004c 2.zip";
//        String sha256_2 = DigestUtils.sha256Hex(new FileInputStream(path2));
//        System.out.println("sha256_2 = " + sha256_2);
//        System.out.println("sha256_1耗时: " + (System.currentTimeMillis() - l_2));
////
//        long l_3 = System.currentTimeMillis();
//        String path3 = "/Users/wlz/Desktop/6be842c8ae014d32a3d327b367b8004c 3.zip";
//        String sha256_3 = DigestUtils.sha256Hex(new FileInputStream(path3));
//        System.out.println("sha256_3 = " + sha256_3);
//        System.out.println("sha256_1耗时: " + (System.currentTimeMillis() - l_3));
    }

}
